package com.voiceblog.action;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.CookiesAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.beans.support.PagedListHolder;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.voiceblog.logic.VoiceBlogFacade;

public class BaseAction extends ActionSupport implements ServletRequestAware,
		SessionAware, CookiesAware, ServletContextAware {
	private HttpServletRequest request;
	private ServletContext context;
	protected VoiceBlogFacade voiceBlogDao;

	public VoiceBlogFacade getVoiceBlogDao() {
		return voiceBlogDao;
	}

	public void setVoiceBlogDao(VoiceBlogFacade voiceBlogDao) {
		this.voiceBlogDao = voiceBlogDao;
	}

	protected Map session;

	// protected Map<Object, Object> cookies;

	public HttpServletRequest getRequest() {
		return request;
	}

	public BaseAction() {

	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}

	public Map getSession() {
		return session;
	}

	public void setSession(Map<String, Object> sessionMap) {
		this.session = sessionMap;
	}

	public void setServletContext(ServletContext context) {
		this.context = context;
		if (context != null) {

			WebApplicationContext wac = WebApplicationContextUtils
					.getRequiredWebApplicationContext(context);
			this.voiceBlogDao = (VoiceBlogFacade) wac.getBean("voiceBlogDao");
		}
	}

	protected VoiceBlogFacade getDao() {
		return voiceBlogDao;
	}

	// public Map<Object, Object> getCookies() {
	// return cookies;
	// }
	//
	// public void setCookies(Map<Object, Object> cookies) {
	// this.cookies = cookies;
	// }
	//
	public void setCookiesMap(Map arg0) {
		// TODO Auto-generated method stub
		// this.cookies = arg0;
	}

	public void pager(String page, PagedListHolder blogList, int pageNum)
			throws Exception {
		if ("next".equals(page)) {

			blogList.nextPage();
		} else if ("previous".equals(page)) {

			blogList.previousPage();
		} else if ("last".equals(page)) {

			blogList.setPage(blogList.getPageCount() - 1);
		} else if ("first".equals(page)) {

			blogList.setPage(0);
		} else if ("other".equals(page)) {
			blogList.setPage(pageNum);
		}
	}

}
